using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.ObjectBuilder;
using NUnit.Framework;
using SCSFContrib.CompositeUI.WPF.Activation;
using SCSFContrib.CompositeUI.WPF.BuilderStrategies;
using SCSFContrib.CompositeUI.WPF.Visualizer;

namespace SCSFContrib.CompositeUI.WPF.UnitTest.Visualizer
{
	[TestFixture]
	public sealed class WindowsVisualizerTest : UnitTest
	{
		private WindowsVisualizer _windowsVisualizer;

		[Test]
		public void AddBuilderStrategies_ShouldAddRequiredStrategies()
		{
			ExecuteInSeparateAppDomain("AddBuilderStrategies_ShouldAddRequiredStrategies_DoWork");
		}

		private void AddBuilderStrategies_ShouldAddRequiredStrategies_DoWork()
		{
			Application application = new Application();
			_windowsVisualizer = new WindowsVisualizer();
			Builder builder = new Builder();
			InvokePrivateMethod<object>(_windowsVisualizer, "AddBuilderStrategies", builder);
			IBuilderStrategyChain chain = builder.Strategies.MakeStrategyChain();
			IBuilderStrategy builderStrategy = chain.Head;
			List<Type> strategyTypes = new List<Type>();

			while (builderStrategy != null)
			{
				strategyTypes.Add(builderStrategy.GetType());
				builderStrategy = chain.GetNext(builderStrategy);
			}

			Assert.IsTrue(strategyTypes.Contains(typeof(WindowsServiceStrategy)));
			Assert.IsTrue(strategyTypes.Contains(typeof(FrameworkElementActivationStrategy)));
			Assert.IsTrue(strategyTypes.Contains(typeof(FrameworkContentElementActivationStrategy)));
			Assert.IsTrue(strategyTypes.Contains(typeof(FrameworkElementSmartPartStrategy)));
		}

		[Test]
		public void AddServices_ShouldAddRequiredServices()
		{
			ExecuteInSeparateAppDomain("AddServices_ShouldAddRequiredServices_DoWork");
		}

		private void AddServices_ShouldAddRequiredServices_DoWork()
		{
			Application application = new Application();
			_windowsVisualizer = new WindowsVisualizer();
			Builder builder = new Builder();
			_windowsVisualizer.Initialize(new TestableRootWorkItem(), builder);
			WorkItem rootWorkItem = GetPrivateMemberValue<WorkItem>(_windowsVisualizer, "RootWorkItem");
			Assert.IsTrue(rootWorkItem.Services.Contains<IFrameworkElementActivationService>());
			Assert.IsTrue(rootWorkItem.Services.Contains<IFrameworkContentElementActivationService>());
		}

		[Test]
		public void CreateVisualizationShell_ShouldCreateAndShowWindow()
		{
			ExecuteInSeparateAppDomain("CreateVisualizationShell_ShouldCreateAndShowWindow_DoWork");
		}

		private void CreateVisualizationShell_ShouldCreateAndShowWindow_DoWork()
		{
			Application application = new Application();
			_windowsVisualizer = new WindowsVisualizer();
			Builder builder = new Builder();
			_windowsVisualizer.Initialize(new TestableRootWorkItem(), builder);

			WorkItem rootWorkItem = GetPrivateMemberValue<WorkItem>(_windowsVisualizer, "RootWorkItem");
			Window visualizerWindow = rootWorkItem.Items["VisualizerWindow"] as Window;
			Assert.IsTrue(visualizerWindow.IsVisible);
		}
	}
}